package com.lagou.edu.controller;

import com.lagou.edu.dao.LagouTokenDao;
import com.lagou.edu.dao.LagouUserDao;
import com.lagou.edu.pojo.LagouAuthCode;
import com.lagou.edu.pojo.LagouToken;
import com.lagou.edu.pojo.LagouUser;
import com.lagou.edu.rpc.CodeFeignClient;
import com.lagou.edu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private LagouUserDao lagouUserDao;
    @Autowired
    private LagouTokenDao lagouTokenDao;
    @Autowired
    private CodeFeignClient codeFeignClient;


    @GetMapping("/test")
    public String test() {
        List<LagouAuthCode> all = userService.findAll();
        return all.get(0).toString();
    }

    /**
     * 注册接⼝
     *
     * @return
     */
    @GetMapping("/register/{email}/{password}/{code}")
    public Boolean register(@PathVariable(value = "email") String email,
                            @PathVariable(value = "password") String password,
                            @PathVariable(value = "code") String code) {
        // 校验验证码
        Integer validate = codeFeignClient.validate(email, code);
        if (validate == 0){
            // 注册
            LagouUser lagouUser = new LagouUser();
            lagouUser.setPassword(password);
            lagouUser.setEmail(email);
            lagouUserDao.save(lagouUser);
        }
        return true;
    }

    /**
     * 是否已注册
     *
     * @return
     */
    @GetMapping("/isRegistered/{email}")
    public Boolean isRegistered(@PathVariable(value = "email") String email) {
        LagouUser lagouUser = new LagouUser();
        lagouUser.setEmail(email);
        Example<LagouUser> example = Example.of(lagouUser);
        Optional<LagouUser> one = lagouUserDao.findOne(example);
        return one.get() == null ? false : true;
    }

    /**
     * 登录接⼝
     *
     * @return 邮箱
     */
    @RequestMapping("/login/{email}/{password}")
    public String login(@PathVariable(value = "email") String email,
                        @PathVariable(value = "password") String password,
                        HttpServletRequest request, HttpServletResponse response) {
        LagouUser lagouUser = new LagouUser();
        lagouUser.setEmail(email);
        lagouUser.setPassword(password);
        Example<LagouUser> example = Example.of(lagouUser);
        Optional<LagouUser> one = lagouUserDao.findOne(example);
        LagouUser lagouUser1 = one.get();

        String uuid = UUID.randomUUID().toString();
        LagouToken lagouToken = new LagouToken();
        lagouToken.setEmail(lagouUser1.getEmail());
        Example<LagouToken> of = Example.of(lagouToken);
        Optional<LagouToken> one1 = lagouTokenDao.findOne(of);
        // 删除之前token
        if (one1.isPresent()) {
            lagouTokenDao.deleteById(one1.get().getId());
        }
        lagouToken.setToken(uuid);
        lagouTokenDao.save(lagouToken);

        // 返回token
        Cookie cookie = new Cookie("token", uuid);
        response.addCookie(cookie);
        return uuid;
    }

    /**
     * 根据token查询⽤户登录邮箱接⼝
     *
     * @return 邮箱
     */
    @GetMapping("/info/{token}")
    public String info(@PathVariable(value = "token") String token) {
        LagouToken lagouToken = new LagouToken();
        lagouToken.setToken(token);
        Example<LagouToken> of = Example.of(lagouToken);
        Optional<LagouToken> one1 = lagouTokenDao.findOne(of);
        return one1.get().getEmail();
    }

}
